<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree
          :data="list"
          default-expand-all
        >
          <template v-slot="{data}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%">
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdi(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children.length===0" @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <el-dialog
      title="添加"
      :visible.sync="showDialog"
      :close-on-press-escape="false"
      :close-on-click-modal="false"
      width="50%"
    >
      <AddorEdit v-if="showDialog" :id="cutid" :originlist="originList" :isedit="isEdit" @success="hSuccess" @clear="hClear" />
    </el-dialog>
    <el-dialog
      title="编辑"
      :visible.sync="showDialogEdi"
      :close-on-press-escape="false"
      :close-on-click-modal="false"
      width="50%"
    >
      <AddorEdit v-if="showDialogEdi" :id="cutid" :originlist="originList" :isedit="isEdit" @success="hSuccess" @clear="hClear" />
    </el-dialog>
  </div>
</template>

<script>
import { getdepartment, deldepartmentById } from '@/api/departments'

import { tranListToTreeData } from '@/utils'

import AddorEdit from './departmentsDialog.vue'
export default {
  components: { AddorEdit },
  data() {
    return {
      // 依赖一份树形数据
      // list: [{
      //   name: '财务部',
      //   manager: '刘备',
      //   children: [
      //     {
      //       name: '财务核算部',
      //       manager: '张飞'
      //     },
      //     {
      //       name: '税务核算部',
      //       manager: '关羽'
      //     }
      //   ]
      // }]
      list: [],
      showDialog: false,
      cutid: '',
      showDialogEdi: false,
      isEdit: false,
      originList: []
    }
  },

  created() {
    this.loadDepartments()
  },
  methods: {
    async  loadDepartments() {
      const res = await getdepartment()
      console.log('数据列表', res)
      res.data.depts.shift()
      this.originList = res.data.depts.map(item => {
        return { id: item.id, code: item.code, name: item.name, pid: item.pid }
      })
      this.list = tranListToTreeData(res.data.depts)
    },
    // 添加弹窗
    hAdd(id) {
      // 打开弹窗
      this.showDialog = true
      // 给子组件传递id
      this.cutid = id
      this.isEdit = false
    },
    hSuccess() {
      // 关闭弹窗
      this.showDialog = false
      this.showDialogEdi = false
      // 重新渲染数据
      this.loadDepartments()
    },
    // 编辑弹窗
    hEdi(id) {
      this.showDialogEdi = true
      this.cutid = id
      this.isEdit = true
    },
    // 点击取消按钮关闭弹窗
    hClear() {
      this.showDialog = false
      this.showDialogEdi = false
    },
    hDel(id) {
      this.$confirm('确定删除此操作吗?', '提示', { confirmButtonText: '确定',
        cancelButtonText: '取消', type: 'warning' }).then(async() => {
        const res = await deldepartmentById(id)
        console.log('删除数据', res)
        this.$message.success(res.message)
        this.loadDepartments()
      }).catch(() => {})
    }
  }
}
</script>
